package org.aplombh.java.leetcode.all;

import java.util.Arrays;
import java.util.HashMap;

public class _1两数之和 {
}

class Error1_1 {
    // 错误、位置会变
    public int[] twoSum(int[] nums, int target) {
        Arrays.sort(nums);
        int i = 0, j = nums.length - 1;
        while (i < j) {
            int t = nums[i] + nums[j];
            if (t == target) break;
            if (t > target) j--;
            else i++;
        }
        int[] res = new int[2];
        res[0] = i;
        res[1] = j;
        return res;
    }
}

class Error1_2 {
    // 错误、可能包含相同的值  6 = 3+3
    public int[] twoSum(int[] nums, int target) {


        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], i);
        }
        for (int i = 0; i < nums.length; i++) {
            int d = target - nums[i];
            if (map.containsKey(d)) return new int[]{i, map.get(d)};
        }
        return null;
    }
}

class Solution1_1 {
    public int[] twoSum(int[] nums, int target) {

        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], i);
        }
        for (int i = 0; i < nums.length; i++) {
            int d = target - nums[i];
            if (map.containsKey(d) && i != map.get(d)) return new int[]{i, map.get(d)};
        }
        return null;
    }
}

class Solution1_2 {
    public int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int d = target - nums[i];
            if (map.containsKey(d) && i != map.get(d)) return new int[]{i, map.get(d)};
            map.put(nums[i], i);
        }
        return null;
    }
}

class Solution1_3 {
    public int[] twoSum(int[] nums, int target) {
        int N = (int) (2 * 10e4 + 1);
        int[] ints = new int[N];
        Arrays.fill(ints, Integer.MAX_VALUE);

        for (int i = 0; i < nums.length; i++) {
            int d = (target - nums[i]);
            if (ints[d] != Integer.MAX_VALUE && i != ints[d]) return new int[]{i, ints[d]};
            ints[i] = (int) (nums[i] + 10e4);
        }
        return null;
    }
}